    import { getCategoryAPI } from '@/network/category';
    import {ref, onMounted} from 'vue'
    import { useRoute, onBeforeRouteUpdate } from 'vue-router';

export function useCategory() {
    
    const route = useRoute()
    const categoryData = ref({})
   async function getCategory(id = route.params.id) {
        const res = await getCategoryAPI(id)
        // console.log(res);
        categoryData.value = res.result
    }

    onMounted(() => {
        getCategory()
    })


     // 监听路由更新
     onBeforeRouteUpdate((to) => {
        // console.log('路有变化了');
        // 使用最新的路由参数id
        // console.log(to);
        getCategory(to.params.id)
    })


    return {categoryData}
}